﻿using System;
using System.Globalization;
using System.Web.UI;
using Business;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class ServiceDetail1 : System.Web.UI.UserControl
    {

        /// <summary>
        /// Occurs when [appeared tab4].
        /// </summary>
        public event EventHandler AppearedTab4;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEndDate.Attributes.Add("onclick", "displayCalendar(document.forms[0]."+txtEndDate.ClientID+",'dd/mm/yyyy',this)");
            txtStartDate.Attributes.Add("onclick", "displayCalendar(document.forms[0]." + txtStartDate.ClientID + ",'dd/mm/yyyy',this)");
            txtStartExpected.Attributes.Add("onclick", "displayCalendar(document.forms[0]." + txtStartExpected.ClientID + ",'dd/mm/yyyy',this)");
            txtStartExpected.TextChanged += TxtStartExpected_TextChanged;
            txtStartDate.TextChanged += TxtStartDate_TextChanged;
            txtEndDate.TextChanged += TxtEndDate_TextChanged;
            ddlMonthExtendable.SelectedIndexChanged += DdlMonthExtendable_SelectedIndexChanged;
            ddlYearExtendable.SelectedIndexChanged += DdlYearExtendable_SelectedIndexChanged;
            chkExtendable.CheckedChanged += ChkExtendable_CheckedChanged;
            chkTimelimited.CheckedChanged += ChkTimelimited_CheckedChanged;
            lbtLookup.Click += LbtLookup_Click;
            ListContact1.CloseControl += ListContact1_CloseControl;
            ListContact1.GetContactId += ListContact1_GetContactId;
            if(pnLeadContact.Visible)
            {
                const string visiblefunction = "$( '#overlay3' ).show();";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "Service1KeyContactEnable1", visiblefunction, true);
            }
            const string draggable = "$( '#draggable' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), UpdatePanel1.ClientID, draggable, true);
            ddlSubtype.SelectedIndexChanged += DdlSubTypeSelectedIndexChanged;

        }
        /// <summary>
        /// Loads the reference data.
        /// </summary>
        public void LoadReferenceData()
        {
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
            ddlServiceType.DataSource = ServiceBusiness.Instance.GetServiceType();
            ddlServiceType.DataTextField = "DataRef";
            ddlServiceType.DataValueField = "RefValue";
            ddlServiceType.DataBind();
            ddlSubtype.DataSource = ServiceBusiness.Instance.GetServiceSubtype();
            ddlSubtype.DataTextField = "DataRef";
            ddlSubtype.DataValueField = "RefValue";
            ddlSubtype.DataBind();
            ddlAttendace.DataSource = ServiceBusiness.Instance.GetServiceAttendance();
            ddlAttendace.DataTextField = "DataRef";
            ddlAttendace.DataValueField = "RefValue";
            ddlAttendace.DataBind();
            ddlProcessMethod.DataSource = ServiceBusiness.Instance.GetProcessMethod();
            ddlProcessMethod.DataTextField = "DataRef";
            ddlProcessMethod.DataValueField = "RefValue";
            ddlProcessMethod.DataBind();




        }

        /// <summary>
        /// DDLs the sub type selected index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void DdlSubTypeSelectedIndexChanged(object sender, EventArgs e)
        {
            if (AppearedTab4 != null)
                AppearedTab4(ddlSubtype.SelectedValue, e);
        }

        /// <summary>
        /// Handles the GetContactId event of the ListContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ListContact1_GetContactId(object sender, EventArgs e)
        {
            var contact = sender as Contact;
            if (contact != null)
            {
                lblLeadContact.ReadOnly = false;
                lblLeadContact.Text = contact.FirstName + " " + contact.SurName;
                lblLeadContact.ReadOnly = true;
                lbtLookup.CommandArgument = contact.ContactId.ToString();
            }
            else
            {
                lblLeadContact.Text = string.Empty;
                lbtLookup.CommandArgument = string.Empty;
            }
        }

        /// <summary>
        /// Handles the CloseControl event of the ListContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ListContact1_CloseControl(object sender, EventArgs e)
        {

            pnLeadContact.Visible = false;
            const string visiblefunction = "$( '#overlay3' ).hide();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "Service1KeyContactEnable2", visiblefunction, true);
            
        }

        /// <summary>
        /// Handles the Click event of the LbtLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtLookup_Click(object sender, EventArgs e)
        {
            const string visiblefunction = "$( '#overlay3' ).show();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "Service1KeyContactEnable3", visiblefunction, true);
            
            pnLeadContact.Visible = true;
        }


        /// <summary>
        /// Handles the CheckedChanged event of the ChkTimelimited control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkTimelimited_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTimelimited.Checked)
            {
                ddlMonthLimit.Enabled = true;
                ddlYearLimit.Enabled = true;
            }
            else
            {
                ddlMonthLimit.Enabled = false;
                ddlYearLimit.Enabled = false;
            }

        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkExtendable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkExtendable_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExtendable.Checked)
            {
                ddlMonthExtendable.Enabled = true;
                ddlYearExtendable.Enabled = true;
            }
            else
            {
                ddlMonthExtendable.Enabled = false;
                ddlYearExtendable.Enabled = false;
            }
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DdlYearExtendable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void DdlYearExtendable_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DdlMonthExtendable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void DdlMonthExtendable_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtEndDate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtEndDate_TextChanged(object sender, EventArgs e)
        {
            DateTime fundraisingCompleteDate;
            if (DateTime.TryParseExact(txtEndDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
            {
                txtEndDate.Text = "";
                txtEndDate.Focus();
            }
            else
            {
                txtEndDate.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture);
            }
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtStartDate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtStartDate_TextChanged(object sender, EventArgs e)
        {
            DateTime fundraisingCompleteDate;
            if (DateTime.TryParseExact(txtStartDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
            {
                txtStartDate.Text = "";
                txtStartDate.Focus();
            }
            else
            {
                txtStartDate.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture);

            }
            chkActive.Enabled = true;
            chkActive.Checked = ValidateDate();
            chkActive.Enabled = false;
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtStartExpected control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtStartExpected_TextChanged(object sender, EventArgs e)
        {
            DateTime fundraisingCompleteDate;
            if (DateTime.TryParseExact(txtStartExpected.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out fundraisingCompleteDate) == false)
            {
                txtStartExpected.Text = "";
                txtStartExpected.Focus();
            }
            else
            {

                txtStartExpected.Text = fundraisingCompleteDate.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture);

            }

        }

        /// <summary>
        /// Validates the date.
        /// </summary>
        /// <returns></returns>
        bool ValidateDate()
        {

            var curentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            DateTime start;
            if (DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out start) == false)
                start = curentDate.AddDays(-2);
            DateTime end;
            if (DateTime.TryParseExact(txtEndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out end) == false)
                end = curentDate.AddDays(2);
            int year;
            if (int.TryParse(ddlYearExtendable.SelectedValue, out year) == false || chkExtendable.Checked == false)
                year = 0;
            int month;
            if (int.TryParse(ddlMonthExtendable.SelectedValue, out month) == false || chkExtendable.Checked == false)
                month = 0;
            end = end.AddMonths(month);
            if (year > 0)
                end = end.AddYears(year);
            if (start <= curentDate && curentDate <= end)
                return true;
            return false;
        }
    }
}